# -*- coding: utf-8 -*-
"""
Created on Mon Dec  1 14:49:45 2014

@author: wei
"""

from flask import Flask,render_template,g
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager,current_user
from werkzeug.contrib.cache import SimpleCache

app = Flask(__name__)
app.config.from_object('config')
db    =SQLAlchemy(app)
cache =SimpleCache()

login_manager              =LoginManager(app)
login_manager.login_view   ='login'
login_manager.login_message='请先登录！'


from app import view
from app.models import Users
from datetime import datetime

@login_manager.user_loader
def load_user(id):
    return Users.query.get(int(id))

@app.before_request
def before_request():
    g.user = current_user
    if g.user.is_authenticated:
        g.user.last_seen = datetime.now()
        db.session.add(g.user)
        db.session.commit()

@app.errorhandler(404)
def internal_404_error(error):
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_500_error(error):
    db.session.rollback()
    return render_template('500.html'), 500
